<template>
  <div>
    <component-head title="登录页"></component-head>
    <div class="loginTable">
        <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="usename"
        label="usename"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="password"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
</div>
  </div>
</template>

<script>
import componentHead from '@/components/componentHead'
import { mapMutations, mapState } from "vuex"
export default {
  data() {
    return {
      username: '',
      password: '',
    }
  },
  computed: {
    ...mapState({
      infos: (state) => state.selfInfo.selfinfo
    })
  },
  methods: {
    onSubmit(values) {
      setTimeout(() => {
        const info = {
          name: '张三',
          photo: "https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1069500154,163694888&fm=111&gp=0.jpg"
        }
          this.setselfInfo(info)
          const tocken = "qwkoqkwkqwq"
          localStorage.setItem('tocken', tocken)
          alert('登录成功')
          // 登录成功后若有上一界面则返回上一界面，没有则去首页
          if(this.$route.params.from){
            this.$router.push(this.$route.params.from)
          }else{
            this.$router.push("/home")
          }
      }, 500);
    },
    ...mapMutations('selfInfo', ['setselfInfo'])
  },
  components: {
    componentHead
  }
}
</script>

<style lang="scss" scoped>
  .van-form{
    margin-top: 46px;
  }
</style>